package com.niit.yhserver.controller;


import com.niit.yhserver.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@RestController
@RequestMapping("/api/admin")
public class LoginController {

    @Autowired
    private AdminService  adminService;
    /**
     * 登录
     */
    @RequestMapping("/login")
    @ResponseBody//@ResponseBody注解在Spring MVC中主要用于将控制器方法的返回值直接写入HTTP响应体中，而不是作为视图名称进行解析‌。
    public String adminAccess(String username,String password,HttpSession session) throws ServletException, IOException {
        String flag = "1";

        System.out.println(username + "---" + password);
        System.out.println(flag);
        if (username == null || "".equals(username) || password == null || "".equals(password)) {
            flag = "-1"; //
            System.out.println(flag);
        } else {
            boolean isSuccess = adminService.login(username, password);
            if (isSuccess) {
                session.setAttribute("username", username);
                System.out.println(username);
                return flag;
            } else {
                flag = "0";
            }
        }
        return flag;
    }

}
